<?php

namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
//推荐列表
class RecommandList extends Model
{
    protected $table='waihui_tuijian_list';
    
    /*该分析师发布文章总篇数
     * $tuijian_id int waihui_tuijian_user表的id 
     * @return int 
     */
    public  function allCountContent($tuijian_id){
        $count = $this->where('tuijian_id',$tuijian_id)->count();
        return $count;
    }
    
    /*该分析师发布所有文章总赞数
     * $tuijian_id int waihui_tuijian_user表的id 
     * @return int 
     */
    public  function allUpvotes($tuijian_id){
        $sum = $this->where('tuijian_id',$tuijian_id)->sum('upvotes');
        return $sum;
    }
    
    /*订阅该用户推荐的分析师的总人数
     * $tuijian_id int waihui_tuijian_user表的id 
     * @return int 
     */
    public function countFollow($tuijian_id){
        $analystId = $this->select('analyst_id')->where('tuijian_id',$tuijian_id)->get()->toArray();
        $ids = [];
        foreach($analystId as $val){
            $id = explode(',',$val['analyst_id']);
            $ids = array_merge($ids,$id);
        }
        $ids = array_unique($ids);
        $count = DB::table('waihui_follow')->where('status',1)->whereIn('analyst_id',$ids)->count();
        return $count;
    }
    
    /*获取每篇文章订阅的总人数
     * $contentId int waihui_tuijian_list表的id 
     * @return int 
     */
    public function contentFollow($contentId){
        $analystId = $this->select('analyst_id')->where('id',$contentId)->first();
        $ids = explode(',',$analystId['analyst_id']);
        $count = DB::table('waihui_follow')->where('status',1)->whereIn('analyst_id',$ids)->count();
        return $count;
    }
    
    
}
